package com.fanbuchi.fukua.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import com.fanbuchi.fukua.R;
import com.fanbuchi.fukua.activity.HomeActivity;
import com.fanbuchi.fukua.activity.base.BaseFragment;
import com.fanbuchi.fukua.api.Oauth2API;
import com.fanbuchi.fukua.api.Oauth2API;
import com.fanbuchi.fukua.api.ProfileAPI;
import com.fanbuchi.fukua.entity.OauthEntity;
import com.fanbuchi.fukua.entity.UserEntity;
import com.fanbuchi.fukua.manager.OauthManager;
import com.fanbuchi.fukua.manager.UserManager;
import com.fanbuchi.fukua.net.BaseCallBack;
import com.fanbuchi.fukua.utils.PassUtils;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by rabbit on 2015/6/27.
 */
public class LoginFragment extends BaseFragment {


    @InjectView(R.id.userName)
    EditText userName;
    @InjectView(R.id.pass)
    AppCompatEditText pass;
    @InjectView(R.id.actionLogin)
    AppCompatButton actionLogin;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }


    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_login, container, false);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @OnClick(R.id.actionLogin)
    public  void actionLoginClick(AppCompatButton button){
        String _userName= userName.getText().toString();
        String _pass=pass.getText().toString();
        FormEncodingBuilder builder=new FormEncodingBuilder()
                .add("userName",_userName)
                .add("password", PassUtils.encode(_pass,_userName));
        try {
            Oauth2API.getAPI().authorize(builder, authorizeCallback());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public Callback authorizeCallback(){
        Callback _callback=  new BaseCallBack<OauthEntity>(OauthEntity.class) {
            @Override
            public void onResponseOk(OauthEntity body) {
                OauthManager.setOauthEntity(body);
                try {
                    ProfileAPI.getAPI().profile(profileCallBack());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        return _callback;
    }


    public Callback profileCallBack(){
        Callback _callback=  new BaseCallBack<UserEntity>(UserEntity.class) {
            @Override
            public void onResponseOk(UserEntity body) {
                UserManager.setUserEntity(body);
                startActivity(HomeActivity.class);
                getActivity().finish();
            }
        };
        return _callback;
    }


}
